package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.common.CustomException;
import com.blog.common.Result;
import com.blog.dto.BlogDto;
import com.blog.pojo.Blog;
import com.blog.pojo.BlogTag;
import com.blog.pojo.Tag;
import com.blog.service.BlogService;
import com.blog.mapper.BlogMapper;
import com.blog.service.BlogTagService;
import com.blog.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author DELL
* @description 针对表【t_blog】的数据库操作Service实现
* @createDate 2022-07-10 15:19:35
*/
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    @Autowired
    private BlogTagService blogTagService;

    @Override
    @Transactional
    public void saveBlogWithTagIds(BlogDto blogDto) {
        //保存基本信息到t_blog
        this.save(blogDto);//此后Id已经生成
        //保存信息到blog和tag的中间表
        this.saveAndUpdateBlogTag(blogDto);
    }

    @Override
    @Transactional
    public void updateBlogWithTagIds(BlogDto blogDto) {
        Blog blogById = this.getById(blogDto.getBlogId());
        //1先删除该博客内容
        //1.1先查寻除所有tag，blog关系表中关于该blog的数据行，对其进行删除
        LambdaQueryWrapper<BlogTag> blogTagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        try {
            blogTagLambdaQueryWrapper.eq(BlogTag::getBlogId,blogById.getBlogId());
        } catch (Exception e) {
            throw new CustomException("您的操作为修改，请携带blogId信息！");
        }
        blogTagService.remove(blogTagLambdaQueryWrapper);
        //1.2删除该t_blog表的原本内容
        this.removeById(blogById);
        //保存新修改的基本信息到t_blog，这里不直接调用this.saveBlogWithTagIds的原因是：增加方法里的blogDto中是没有blogId的，而更新时需要。
        this.save(blogDto);
        //保存新修改的信息到blog和tag的中间表
        this.saveAndUpdateBlogTag(blogDto);
    }

    /**
     * 逻辑删除博客
     * @param blogId
     */
    @Override
    @Transactional
    public String updateDeleteStatus(Long blogId) {
        Blog blogServiceById = this.getById(blogId);
        blogServiceById.setIsDelete(1);
        //删除对应博客的全部标签
        LambdaQueryWrapper<BlogTag> tagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tagLambdaQueryWrapper.eq(BlogTag::getBlogId,blogId);
        blogTagService.remove(tagLambdaQueryWrapper);
        return "注意：该博客逻辑删除成功！但恢复后该博客下的所有标签会丢失。";
    }

    @Override
    public Page<Blog> page(int page, int pageSize) {
        Page<Blog> blogPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Blog> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按博客发布（其实是草稿）时间降序排
        blogLambdaQueryWrapper.orderByDesc(Blog::getCreateTime);
        return this.page(blogPage,blogLambdaQueryWrapper);
    }

    /**
     * 增添和更新的公共方法：保存中间表
     * @param blogDto
     */
    private void saveAndUpdateBlogTag(BlogDto blogDto){
        //构造查询条件先获得blogId
        LambdaQueryWrapper<Blog> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogLambdaQueryWrapper.eq(Blog::getTitle,blogDto.getTitle());
        Blog blog = this.getOne(blogLambdaQueryWrapper);
        Long blogId = blog.getBlogId();
        Long[] tagList = blogDto.getTagList();
        List<BlogTag> blogTagList = new ArrayList<>();
        for (Long s : tagList) {
            BlogTag blogTag = new BlogTag();
            blogTag.setBlogId(blogId);
            blogTag.setBlogTagId(s);
            blogTagList.add(blogTag);
        }
        //保存到中间表
        blogTagService.saveBatch(blogTagList);
    }
}





